<template> 
    <div class="content" >
        <div class="login">
            <h1>用户登录</h1>
            <div style="margin-top: 25px;"></div>
            <el-form ref="loginForm" label-width="100px" :model="rule" style="max-width: 460px">
                <el-form-item label="用户名:" prop="name">
                    <el-input v-model="rule.name" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="密码:" prop="password">
                    <el-input v-model="rule.password" type="password" placeholder="请输入密码" show-password />
                </el-form-item>
                <div style="margin-top: 25px;"></div>
            </el-form>
            <el-button id="dl" type="primary" @click="login">登录</el-button>
            <el-button id="cz" type="info" @click="resetForm">重置</el-button>
            <a href="/zhuce" class="zc">注册</a>
        </div>
    </div>
</template>
<script>
import {  ref } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios';
import { ElMessage } from 'element-plus'
export default{
    setup(){
    const rule = ref({ name:"",password:""});
    const router = useRouter();
    function login(){
    const name = rule.value.name;
    const password = rule.value.password;
    axios
    .get(`/api/api/Login?username=${encodeURIComponent(name)}&password=${encodeURIComponent(password)}`)
    .then((res)=>{
        console.log(res)
        if(res.data!=1 && res.data!=0){
            sessionStorage.setItem("token",res.data);
            ElMessage({
                message:'登录成功',
                type:'success'
            })
            router.push({path:"/home"})
        }else{
            ElMessage.error('账号或密码错误')
            resetForm();//重置
         }
        })
        .catch((error) => {
      console.error(error)
    })
        }
        //重置
        function resetForm() {
            rule.value.name = "";
            rule.value.password = "";
        }
        return {
            rule,
            login,
            resetForm,
        };
    },
};

</script>
<style>
h1{
color: #5C5F62;
font-size: 40px;
}
* {
    margin: 0px;
    padding: 0px;
}
.zc{
    color: white;
}
#dl{
    width: 150px;
    height: 40px;
    margin-left: 120px;
}
#cz{
    width: 150px;
    margin-left: 18px;
    height: 40px;
}
.content {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}
/* 毛边效果 */
.login {
  margin-top: 270px;
  width: 500px;
  height: 300px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.2);
  padding: 20px;
  backdrop-filter: blur(10px);
}

.login>h1 {
    text-align: center;
}

.login el-form-item {
  margin-bottom: 20px;
}
.content {
  width: 100%;
  height: 100vh;
  background-image: url('../assets/黑海.jpg');
  background-size: cover;
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>
